package com.example.kotlinview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.example.kotlinview.databinding.ActivityRoomBinding
import com.example.kotlinview.room.AppDataBase
import com.example.kotlinview.room.User
import com.example.kotlinview.room.UserDao
import com.example.utils.ToastUtil
import kotlin.concurrent.thread

class RoomActivity : AppCompatActivity() {
    var userDao: UserDao?= null;
    var user1:User?=null;
    var user2:User?=null;
    var user3:User?=null;
    lateinit  var roomModel :RoomModel
    private lateinit var binding: ActivityRoomBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        roomModel = ViewModelProvider(this).get(RoomModel::class.java)
        binding = ActivityRoomBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.btnGet.setOnClickListener {
            ToastUtil.showToast("viewbinding.btnGet")
        }

        userDao= AppDataBase.getDatabase(this).UserDao()
        user1 = User("haha", "hehe", 4)
        user2 = User("xixi", "hihi", 4)
        user3 = User("san", "liu",3)


    }

//    控件实例化和onClick实现 同时设置，
//    优先响应控件实例化
    fun get(view: View) {}
    fun query(view: View) {
        thread {
            Log.e("RoomActivity", "查询")
            query();
        }
    }
    fun delete_user1(view: View) {
        thread {
            Log.e("RoomActivity", "删除1")
            user1?.let { userDao?.deleteUserByLastName(it.lastName) }
            query()
        }
    }
    fun delete_user2(view: View) {
        thread {
            Log.e("RoomActivity", "删除2")
            user2?.let { userDao?.deleteUserByLastName(it.lastName) }
            query()
        }
    }

    fun delete(view: View) {
        thread {
            Log.e("RoomActivity", "删除3")
            user3?.let { userDao?.deleteUserByLastName(it.lastName) }
            query()
        }
    }
    fun update(view: View) {
        thread {
            Log.e("RoomActivity", "更新")
            user1?.let {
                user1?.age=44
                userDao?.updateUser(it)
                query()

            }


        }
    }
    fun insert(view: View) {
        thread {
            Log.e("RoomActivity", "插入")
            user1?.let {
//                room执行插入操作要返回结果给主键id，
//                否则update无效
                user1!!.id= userDao?.insertUser(it)!!
            }
            user2?.let {
                user2!!.id= userDao?.insertUser(it)!!
            }
            user3?.let {
                user3!!.id= userDao?.insertUser(it)!!
            }
            query()
        }
    }
    fun query(){

          var userMessage= StringBuffer().apply {
                for (user in userDao?.loadAllUsers()!!){
                    Log.e("RoomActivity", user.toString())
                    append(user.id)
                        .append(user.toString())
                        .append("\n")
            }
        }
        runOnUiThread {
            binding.tvRoomMessage.text=userMessage
        }
    }


}